﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using DAL;
using System.Web.Script.Serialization;
using System.Data.SqlClient;

namespace 公司CRM客户关系系统.Customer
{
    /// <summary>
    /// LostsPage 的摘要说明
    /// </summary>
    public class LostsPage : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["action"];
            if (action == "UpdateCustomLosts")
            {//CustomLosts 表的修改
                int CLID = int.Parse(context.Request["CLID"]);
                string CLReason = context.Request["CLReason"];
                CustomLosts cl = new CustomLosts();
                cl.CLID = CLID;
                cl.CLReason = CLReason;
                CustomLosts C = BaseDAL.Find<CustomLosts>(CLID);
                string CusID = C.CusID;
                cl.CusID = CusID;
                cl.CLOrderDate = C.CLOrderDate;
                cl.CLDate = C.CLDate;
                cl.CLEnterDate = DateTime.Now;
                cl.CLState = Convert.ToInt32(3);
                cl.ChanID = C.ChanID;
                //得到全部修改数据
                int r = BaseDAL.Update(cl);
                //Customers表的修改客户状态状态
                //查询
                string sql = "select * from Customers where CusID='" + CusID+"'";
                List<Customers> CS = BaseDAL.Select<Customers>(sql, null);
                Customers ct = new Customers();
                foreach(Customers cut in CS){
                    ct.ID = cut.ID;
                    ct.CusID = cut.CusID;
                    ct.UserID = cut.UserID;
                    ct.CusName = cut.CusName;
                    ct.CusAddress = cut.CusAddress;
                    ct.CusZip = cut.CusZip;
                    ct.CusFax = cut.CusFax;
                    ct.CusWebsite = cut.CusWebsite;
                    ct.CusLicenceNo = cut.CusLicenceNo;
                    ct.CusChieftain = cut.CusChieftain;
                    ct.CusBankroll = cut.CusBankroll;
                    ct.CusTurnover = cut.CusTurnover;
                    ct.CusBank = cut.CusBank;
                    ct.CusBankNo = cut.CusBankNo;
                    ct.CusLocalTaxNo = cut.CusLocalTaxNo;
                    ct.CusNationalTaxNo = cut.CusNationalTaxNo;
                    ct.CusDate = cut.CusDate;
                    ct.CusState = Convert.ToInt32(2);  
                }
                //得到全部修改数据
                int t = BaseDAL.Update(ct);

                //返回 CustomLosts表的结果
                context.Response.Write(r);
            }
            else if (action == "AddMeasures")
            {
                int CLID = int.Parse(context.Request["CLID"]);
                string MeDesc = context.Request["MeDesc"];
                Measures m = new Measures();
                m.CLID = CLID;
                m.MeDesc = MeDesc;
                m.MeDate = DateTime.Now;

                int r = BaseDAL.Insert(m);
                context.Response.Write(r);
            }
            else if (action == "find")
            {
                //得到ID
                int CLID = int.Parse(context.Request["CLID"]);
                
                //查询
                List<Measures> m = BaseDAL.Select<Measures>("select * from Measures where CLID=" + CLID, null);
                //转为json
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(m);
                //输出结果
                context.Response.Write(json);
            }
            else if (action == "find1")
            {
                //得到CusID
                string CusID = context.Request["CusID"];

                //SqlParameter[] pars = new SqlParameter[1];
                //pars[0] = new SqlParameter("@CusID", "%" + CusID + "%");
                string sql = "select * from LinkMans where CusID="+CusID;
                //查询
                List<LinkMans> m = BaseDAL.Select<LinkMans>(sql, null);
                //转为json
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(m);
                //输出结果
                context.Response.Write(json);
            }
            else if (action == "find2")
            {
                //得到CusID
                string CusID = context.Request["CusID"];
                //查询
                List<Activitys> m = BaseDAL.Select<Activitys>("select * from Activitys where CusID=" + CusID, null);
                //转为json
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(m);
                //输出结果
                context.Response.Write(json);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}